package com.yzk.web.controller.system;

import cn.hutool.http.HttpRequest;
import org.apache.commons.lang3.StringUtils;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


public class GPController{



    public static void main(String[] args) {
//        getTc("sz", 1000, 1200);

//        String params="sh600368,sz002367,sz002124,sh688518";


        String params = "sh600483";
//        String params = "sz000933,sz000932,sz000928,sz000623,sz000598,sz000672,sz000682,sz000636,sz000680,sz000651";
//        String params ="sh600188,sh600276,sh600123,sh600236,sh600519,sh600406,sh600348,sh600377,sh600398,sh600483,sh600489,sh600436,sh600309,sh600487,sh601088,sh601012,sh601009,sh601126,sh601001,sh601127,sh601018,sh601138,sh601166,sh601179,sh601225,sh601318,sh601288,sz000333,sz000338,sz000625,sz000651,sz000733,sz000858,sz000988,sz000983,sz000708,sz000726,sz000729,sz000733,sz000738,sz000739,sz000737,sz000758,sz000768,sz000776,sz000786,sz000807,sz000811,sz000821,sz000837,sz000848,sz000858,sz000875,sz000885,sz000887,sz000895,sz000877,sz000878,sz000902,sz000915,sz000913,sz000920,sz000921,sz000922,sz000932,sz000928,sz000933,sz000938,sz000951,sz000957,sz000963,sz000977,sz001328,sz001338,sz000001,sz000021,sz000034,sz000039,sz000065,sz000157,sz000166,sz000568,sz000598,sz000623,sz000636,sz000672,sz000680,sz000682";
        getStockList(params);
    }

    //获取腾讯股票数据
    public static void getTc(String szOrSh, int index, int number) {

        //000001 循环到000050，按逗号分割
        String params = "";
        List<String> codeList = new ArrayList<>();
        for (int i = index; i < number; i++) {
            String code = null;
            //判断i的长度，补充0

            if (i < 10) {
                code = szOrSh + "00000" + i;

            } else if (i < 100) {
                code = szOrSh + "0000" + i;
            } else if (i < 1000) {
                code = szOrSh + "000" + i;
            } else if (i < 10000) {
                code = szOrSh + "00" + i;
            }

            codeList.add(code);

            if ("sh".equals(szOrSh)) {
                if (i < 10) {
                    code = szOrSh + "60000" + i;

                } else if (i < 100) {
                    code = szOrSh + "6000" + i;
                } else if (i < 1000) {
                    code = szOrSh + "600" + i;
                } else if (i < 10000) {
                    code = szOrSh + "60" + i;
                }
                codeList.add(code);
            }


        }

        if (CollectionUtils.isEmpty(codeList)) {
            return;
        }
        int packet = 100;//每100调用一次接口
        int size = codeList.size();
        int j = 0;
        for (int i = 0; i < size; i += packet) {
            j++;
//            System.out.println("=======================第" + j + "轮begin=====================");
            int k = Math.min(i + packet, size);
            List<String> subCodeList = codeList.subList(i, k);
            params = StringUtils.join(subCodeList, ",");
            getStockList(params);
//            System.out.println("=======================第" + j + "轮end=====================");
        }


    }

    public static void getStockList(String params) {


//        System.out.println(params);
        //sh600000
        String url = "http://qt.gtimg.cn/q=" + params;
        String resp = HttpRequest.get(url)
                .execute().body();

        System.out.println("resp:"+ resp);

        if (StringUtils.isEmpty(resp)) {
            System.out.println("返回为空");
            return;
        }
        //1、先按“;”分隔，stock
        String[] stockList = resp.split(";");

        if (stockList.length < 1) {
            System.out.println("股票返回为空");
            return;
        }

        int num = 0;
        for (String stock : stockList) {
            num++;
            //2.按“=”号分割
            String[] stockInfo = stock.split("=");
            if (stockInfo.length < 2) {

                return;
            }
            String stockDetail = stockInfo[1];
            String[] items = stockDetail.split("~");
            if (items.length > 1) {
                //股票名称
                String stockName = items[1];
                //股票代码
                String stockCode = items[2];
                //当前价格
                String stockPrice = items[3];
                //昨收价格
                String stockYClose = items[4];
                //今开价格
                String stockOpen = items[5];
                //	成交量 (手)
                String stockVolume = items[6];
                //外盘 (主动买入，手)
                String stockForeign = items[7];
                //内盘 (主动卖出，手)
                String stockInner = items[8];
                //*****五档盘口****
                //买一价
                String stockBuy1 = items[9];
                //买一量 (手)
                String stockBuy1Volume = items[10];
                //买二价
                String stockBuy2 = items[11];
                //买二量 (手)
                String stockBuy2Volume = items[12];
                //买三价
                String stockBuy3 = items[13];
                //买三量 (手)
                String stockBuy3Volume = items[14];
                //买四价
                String stockBuy4 = items[15];
                //买四量 (手)
                String stockBuy4Volume = items[16];
                //买五价
                String stockBuy5 = items[17];
                //买五量 (手)
                String stockBuy5Volume = items[18];
                //卖一价
                String stockSell1 = items[19];
                //卖一量 (手)
                String stockSell1Volume = items[20];
                //卖二价
                String stockSell2 = items[21];
                //卖二量 (手)
                String stockSell2Volume = items[22];
                //卖三价
                String stockSell3 = items[23];
                //卖三量 (手)
                String stockSell3Volume = items[24];
                //卖四价
                String stockSell4 = items[25];
                //卖四量 (手)
                String stockSell4Volume = items[26];
                //卖五价
                String stockSell5 = items[27];
                //卖五量 (手)
                String stockSell5Volume = items[28];
                //29	(空)	未知
                String stockUnknown = items[29];
                //时间
                String stockTime = items[30];
                //涨跌额 (元)
                String stockChange = items[31];
                //涨跌幅 (%)
                String stockChangePercent = items[32];
                //最高价
                String stockHigh = items[33];
                //最低价
                String stockLow = items[34];
                //价格/成交量(手)/成交额(元)
                String stockPriceVolume = items[35];
                //成交量(手)
                String stockVolume2 = items[36];
                //成交额(万元)
                String stockAmount = items[37];
                //换手率 (%)
                String stockChangeHand = items[38];
                //市盈率 (动态)
                String stockPE = items[39];

                //输出以上所有的值
                System.out.println("============================" + num + "===================================");
                System.out.println("股票名称：" + stockName);
                System.out.println("股票代码：" + stockCode);
                System.out.println("当前价格：" + stockPrice);
                System.out.println("昨收价格：" + stockYClose);
                System.out.println("今开价格：" + stockOpen);
                System.out.println("成交量 (手)：" + stockVolume);
                System.out.println("外盘 (主动买入，手)：" + stockForeign);
                System.out.println("内盘 (主动卖出，手)：" + stockInner);
                System.out.println("买一价：" + stockBuy1);
                System.out.println("买一量 (手)：" + stockBuy1Volume);
                System.out.println("买二价：" + stockBuy2);
                System.out.println("买二量 (手)：" + stockBuy2Volume);
                System.out.println("买三价：" + stockBuy3);
                System.out.println("买三量 (手)：" + stockBuy3Volume);
                System.out.println("买四价：" + stockBuy4);
                System.out.println("买四量 (手)：" + stockBuy4Volume);
                System.out.println("买五价：" + stockBuy5);
                System.out.println("买五量 (手)：" + stockBuy5Volume);
                System.out.println("卖一价：" + stockSell1);
                System.out.println("卖一量 (手)：" + stockSell1Volume);
                System.out.println("卖二价：" + stockSell2);
                System.out.println("卖二量 (手)：" + stockSell2Volume);
                System.out.println("卖三价：" + stockSell3);
                System.out.println("卖三量 (手)：" + stockSell3Volume);
                System.out.println("卖四价：" + stockSell4);
                System.out.println("卖四量 (手)：" + stockSell4Volume);
                System.out.println("卖五价：" + stockSell5);
                System.out.println("卖五量 (手)：" + stockSell5Volume);
                System.out.println("时间：" + stockTime);
                System.out.println("涨跌额 (元)：" + stockChange);
                System.out.println("涨跌幅 (%)：" + stockChangePercent);
                System.out.println("最高价：" + stockHigh);
                System.out.println("最低价：" + stockLow);
                System.out.println("价格/成交量(手)/成交额(元)：" + stockPriceVolume);
                System.out.println("成交量(手)：" + stockVolume2);
                System.out.println("成交额(万元)：" + stockAmount);
                System.out.println("换手率 (%)：" + stockChangeHand);
                System.out.println("市盈率 (TTM)：" + stockPE);


                System.out.println("40:"+items[40]);
                System.out.println("最高:"+items[41]);
                System.out.println("最低:"+items[42]);
                System.out.println("振幅（%）:"+items[43]);
                System.out.println("流通市值（亿）:"+items[44]);
                System.out.println("总市值(亿):"+items[45]);
                System.out.println("市净率:"+items[46]);
                System.out.println("涨停:"+items[47]);
                System.out.println("跌停:"+items[48]);
                System.out.println("量比:"+items[49]);
                System.out.println("50:"+items[50]);
                System.out.println("均价:"+items[51]);
                System.out.println("市盈（动）:"+items[52]);
                System.out.println("市盈（静）:"+items[53]);
                System.out.println("54:"+items[54]);
                System.out.println("55:"+items[55]);
                System.out.println("56:"+items[56]);
                System.out.println("成交额（万元）:"+items[57]);
                System.out.println("58:"+items[58]);
                System.out.println("59:"+items[59]);
                System.out.println("60:"+items[60]);
                System.out.println("61:"+items[61]);
                System.out.println("62:"+items[62]);
                System.out.println("63:"+items[63]);
                System.out.println("股息率TTM:"+items[64]);
                System.out.println("65:"+items[65]);
                System.out.println("66:"+items[66]);
                System.out.println("67:"+items[67]);
                System.out.println("68:"+items[68]);
                System.out.println("69:"+items[69]);
                System.out.println("70:"+items[70]);
                System.out.println("71:"+items[71]);
                System.out.println("流通股本（元）:"+items[72]);
                System.out.println("总股本（元）:"+items[73]);
                System.out.println("委比:"+items[74]);
                System.out.println("75:"+items[75]);
                System.out.println("流通股本:"+items[76]);
                System.out.println("77:"+items[77]);
                System.out.println("78:"+items[78]);
                System.out.println("79:"+items[79]);
                System.out.println("80:"+items[80]);

                System.out.println("2025年上半年净资产收益率为:%");


            }
        }
    }
}
